<template>
    <div class="me">
        <div class="me-container">
            <div class="bg-layer" v-if="showLayer || showMember" :style="{height:clientHeight+'rem'}"></div>
            <div class="me-header">
                <img class="me-header-bg" src="~assets/images/avatar_bg.jpg" alt="">
                <div class="user-msg">
                    <div class="user-avatar">
                        <div class="avatar">
                            <img :src="userInfo.headimgurl" alt="">
                        </div> 
                        <p>{{userInfo.nickname}}</p>
                    </div>
                    <div class="user-rights">
                        <div class="rights">
                            <span>{{userInfo.point}}</span>
                            <span>累计积分</span>
                        </div>
                        <div class="line"></div>
                        <div class="rights" @click="showMemberHandle">
                            <img src="~assets/icons/me_icon1.png" alt="">
                            <span>会员卡</span>
                        </div>
                        <div class="line"></div>
                        <div class="rights" @click="toCollect">
                            <img src="~assets/icons/me_icon2.png" alt="">
                            <span>收藏</span>
                        </div>
                        <div class="line"></div>
                        <div class="rights" @click="toTicket">
                            <img src="~assets/icons/me_icon3.png" alt="">
                            <span>优惠券</span>
                        </div>
                    </div>
                </div>
            </div>
            <div class="me-store">
                <div class="store-header" @click="toAllOrder">
                    <span>我的订单</span>
                    <div class="all-store">
                        <span>全部订单</span>
                        <img src="~assets/icons/arrow.png" alt="">
                    </div>
                </div>
                <div class="store-list">
                    <div class="classify" @click="toWaitPayment">
                        <img src="~assets/icons/detail_icon1.png" alt="">
                        <span>待付款</span>
                    </div>
                    <div class="classify" @click="toWaitDeliver">
                        <img src="~assets/icons/detail_icon2.png" alt="">
                        <span>待发货</span>
                    </div>
                    <div class="classify" @click="toWaitGoods">
                        <img src="~assets/icons/detail_icon3.png" alt="">
                        <span>已发货</span>
                    </div>
                    <div class="classify" @click="toWaitComment">
                        <img src="~assets/icons/detail_icon4.png" alt="">
                        <span>待评价</span>
                    </div>
                    <div class="classify" @click="toRefund">
                        <img src="~assets/icons/detail_icon5.png" alt="">
                        <span>退换货</span>
                    </div>
                </div>
            </div>
            <div class="me-others">
                <div class="others" @click="showAddress">
                    <div class="left">
                        <div class="img-box">
                            <img src="~assets/icons/detail_icon6.png" alt="">
                        </div>
                        <span>地址管理</span>
                    </div>
                    <div class="right">
                        <img src="~assets/icons/arrow.png" alt="">
                    </div>
                </div>
                <div class="others" @click="showDistribution">
                    <div class="left">
                        <div class="img-box">
                            <img src="~assets/icons/detail_icon7.png" alt="">
                        </div>
                        <span>分销中心</span>
                    </div>
                    <div class="right">
                        <img src="~assets/icons/arrow.png" alt="">
                    </div>
                </div>
                <div class="others">
                    <div class="left">
                        <div class="img-box">
                            <img src="~assets/icons/detail_icon8.png" alt="">
                        </div>
                        <span>客服</span>
                    </div>
                    <div class="right">
                        <img src="~assets/icons/arrow.png" alt="">
                    </div>
                </div>
            </div>
            <div class="distribution-layer" v-if="showLayer">
                <div class="distribution">
                    <div class="distribution-head">
                        <span>分销注册</span>
                    </div>
                    <div class="distribution-form">
                        <div class="input-msg">
                            <span>注册分销</span>
                            <input type="text" placeholder="填写手机号码" v-model="distributorInfo.phone">
                        </div>
                        <div class="input-msg">
                            <span>验证码</span>
                            <input type="text" placeholder="填写短信验证码" v-model="distributorInfo.code">
                            <div class="message-verify" @click="getVerifyCode">
                                <span>获取验证码</span>
                            </div>
                        </div>
                        <div class="input-msg">
                            <span>实名认证</span>
                            <input type="text" placeholder="输入真实姓名" v-model="distributorInfo.name">
                        </div>
                        <div class="input-msg">
                            <span>有效证件</span>
                            <input type="text" placeholder="输入身份证号码" v-model="distributorInfo['id_number']">
                        </div>
                    </div>
                    <div class="config-message">
                        <p class="jump-to-protocal">查看《康不离商城分销协议》</p>
                        <div class="config-check">
                            <input type="checkbox" id="read-protocal" @change="readProtocolDis($event)">
                            <label class="protocal" for="read-protocal">
                                我已阅读并接受<span>《康不离商城分销协议》</span>
                            </label>
                        </div>
                    </div>
                    <div class="config-btn" @click="registerDis" :class="{readed:readDis == false}">
                        <span>立即注册</span>
                    </div>
                </div>
                <div class="close-layer">
                    <img src="~assets/icons/close_layer.png" alt="" @click="hiddenLayer">
                </div>
            </div>
            <div class="member-layer" v-if="showMember">
                <div class="distribution">
                    <div class="distribution-head">
                        <span>会员注册</span>
                    </div>
                    <div class="distribution-form">
                        <div class="input-msg">
                            <span>注册会员</span>
                            <input type="text" placeholder="填写手机号码" v-model="member.tel">
                        </div>
                        <div class="input-msg">
                            <span>验证码</span>
                            <input type="text" placeholder="填写短信验证码" v-model="member.verify">
                            <div class="message-verify" @click="getVerify">
                                <span>获取验证码</span>
                            </div>
                        </div>
                    </div>
                    <div class="config-message">
                        <p class="jump-to-protocal">查看《康不离商城会员协议》</p>
                        <div class="config-check">
                            <input type="checkbox" id="read-protocal" @change="readProtocol($event)">
                            <label class="protocal" for="read-protocal">
                                我已阅读并接受<span>《康不离商城会员协议》</span>
                            </label>
                        </div>
                    </div>
                    <div class="config-btn" @click="toMemberCenter" :class="{readed:readPro == false}">
                        <span>立即注册</span>
                    </div>
                </div>
                <div class="close-layer">
                    <img src="~assets/icons/close_layer.png" alt="" @click="hiddenMemberLayer">
                </div>
            </div>
        </div>
    </div>
</template>

<script>
export default {
    name:'me',
    data(){
        return {
            showLayer:false,
            showMember:false,
            clientHeight:0,
            distribution:true,
            member:{
                tel:'',
                verify:''
            },
            readPro: false,
            readDis: false,
            userInfo:{},
            distributorInfo:{
                phone:'',
                id_number:'',
                name:'',
                code:''
            }
        }
    },
    methods:{
        toAllOrder(){
            this.$router.push('/order');
            this.$store.state.oldTab = this.$store.state.tab;
            this.$store.state.tab = '我的订单';
        },
        hiddenLayer(){
            this.showLayer = false;
        },
        showAddress(){
            this.$router.push('/address/list');
            this.$store.state.oldTab = this.$store.state.tab;
            this.$store.state.tab = '地址管理';
        },
        showDistribution(){
            let that  = this;
            let token = localStorage.getItem('token');
            let loader = that.loading();
            that.$ajax(that, 'get', that.$api.distributionOrNot, null, token, function(res){
                if(res.data['error_code'] == 0){
                    if(res.data['msg']){
                        localStorage.setItem('distribution', res.data['msg']);
                        that.$router.push('/distribution');
                        that.$store.state.oldTab = that.$store.state.tab;
                        that.$store.state.tab = '分销中心'; 
                    }else{
                        that.showLayer = true;
                    }
                }else{
                    that.toast(res.data['msg']);
                }
                loader.close();
            })
        },
        showMemberHandle(){
            // 判断用户是否为会员
            let that = this;
            let token = localStorage.getItem('token');
            let loader = that.loading('发送中...');
            that.$ajax(that, 'get', that.$api.member, null, token, function(res){
                if(res.data['error_code'] == 0){
                    that.$router.push('/members/privilege');
                }else{
                    this.showMember = true;
                }
                loader.close();
            })
        },
        hiddenMemberLayer(){
            this.showMember = false;
        },
        toMemberCenter(){
            if(this.readPro == false || this.member.tel == '' || this.member.verify == ''){
                this.toast('请完整填写信息！');
                return;
            }else{
                let that = this;
                let token = localStorage.getItem('token');
                let loader = that.loading('注册中...');
                that.$ajax(that, 'post', that.$api.registerMember, {phone:that.member.tel, code:that.member.verify}, token, function(res){
                    if(res.data['error_code'] == 0){
                        that.$router.push('/members/privilege');
                    }else{
                        that.toast(res.data['msg']);
                    }
                    loader.close();
                })               
            }
        },
        toCollect(){
            this.$router.push('/collect');
        },
        toTicket(){
            this.$router.push('/ticket');
        },
        toWaitPayment(){
            this.$router.push('/order/wait_payment');
        },
        toWaitDeliver(){
            this.$router.push('/order/wait_deliver');
        },
        toWaitGoods(){
            this.$router.push('/order/wait_goods');
        },
        toWaitComment(){
            this.$router.push('/order/wait_comment');
        },
        toRefund(){
            this.$router.push('/order/refund');
        },
        readProtocol(ev){
            console.log(ev);
            let checked = ev.srcElement.checked;
            if(checked){
                this.readPro = true;
            }else{
                this.readPro = false;
            }
        },
        readProtocolDis(ev){
            let checked = ev.srcElement.checked;
            if(checked){
                this.readDis = true;
            }else{
                this.readDis = false;
            }
        },
        getVerify(){
            let that = this;
            let token = localStorage.getItem('token');
            if(that.member.tel == ''){
                return;
            }
            that.$ajax(that, 'post', that.$api.getCode, {phone:that.member.tel}, token, function(res){
                if(res.data['error_code'] != 0){
                    that.toast(res.data['msg']);
                }
            })
        },
        getUserInfo(){
            let that = this;
            let token = localStorage.getItem('token');
            let loader = that.loading();
            that.$ajax(that, 'get', that.$api.userInfo, null, token, function(res){
                if(res.data['error_code'] == 0){
                    that.userInfo = res.data.msg;
                }else{
                    that.toast(re.data.msg)
                }
                loader.close();
            })
        },
        registerDis(){
            if(this.distributorInfo.phone == '' || this.distributorInfo.id_number == '' || this.distributorInfo.name == '' || this.distributorInfo.code == ''){
                this.toast('请完整填写信息！');
                return;
            }
            let that = this;
            let token = localStorage.getItem('token');
            that.$ajax(that, 'post', that.$api.registerDis, that.distributorInfo, token, function(res){
                if(res.data['error_code'] == 0){
                    that.$router.push('/distribution');
                }else{
                    that.toast(res.data.msg);
                }
                loader.close();
            })
        },
        getVerifyCode(){
            if(this.distributorInfo.phone == ''){
                this.toast('请填写手机号码！');
                return;
            }
            let that = this;
            let token = localStorage.getItem('token');
            let loader = that.loading('信息发送中...');
            that.$ajax(that, 'post', that.$api.getCode, {phone:that.distributorInfo.phone}, token, function(res){
                if(res.data['error_code'] != 0){
                    that.toast(res.data['msg']);
                }
                loader.close();
            })
        }
    },
    mounted(){
        let scale = window.devicePixelRatio;
        let clientHeight = document.documentElement.clientHeight;
        this.clientHeight = clientHeight*scale/100;
        this.$store.state.tab = '个人中心';
        this.getUserInfo();
    },
    activated(){
        this.$store.state.tab = '个人中心';
    }
}
</script>

<style lang='less'>
@rem:100rem;
.me{
    width: 750/@rem;
    margin-bottom: 96/@rem;
    margin-top: 88/@rem;
    .me-container{
        width: 100%;
        .bg-layer{
            position: fixed;
            left: 0;
            top: 0;
            z-index: 9999;
            width: 750/@rem;
            background: rgba(0,0,0,0.3);
        }
        .distribution-layer{
            position: fixed;
            left: 100/@rem;
            top: 360/@rem;
            z-index: 99;
            width: 550/@rem;
            .distribution{
                width: 550/@rem;
                height: 625/@rem;
                border-radius: 30/@rem;
                background: #fff;
                display: flex;
                flex-direction: column;
                align-items: center;
                .distribution-head{
                    margin-top: 20/@rem;
                    span{
                        font-size: 36/@rem;
                        letter-spacing: 4/@rem;
                        font-weight: 500;
                        color: transparent;
                        background-image: linear-gradient(180deg, 
                        #35a9fe 0%, 
                        #916ffe 85%, 
                        #ed34fe 100%), 
                        linear-gradient(
                            #5b5b5b, 
                            #5b5b5b);
                        -webkit-background-clip: text;
                        -webkit-text-fill-color: transparent;
                    }
                }
                .distribution-form{
                    margin-top: 20/@rem;
                    .input-msg{
                        width: 490/@rem;
                        height: 76/@rem;
                        box-sizing: border-box;
                        padding-top: 16/@rem;
                        display: flex;
                        align-items: center;
                        border-bottom: 1px solid #cccccc;
                        span{
                            display: inline-block;
                            width: 144/@rem;
                            text-align: start;
                        }
                        input{
                            height: 30/@rem;
                            width: 330/@rem;
                            font-size: 24/@rem;
                            border: none;
                            outline: none;
                        }
                    }
                    .input-msg:nth-of-type(2){
                        input{
                            width: 170/@rem;
                            margin-right: 20/@rem;
                            border: none;
                            outline: none;
                        }
                        .message-verify{
                            width: 148/@rem;
                            height: 38/@rem;
                            border-radius: 4/@rem;
                            background: #6699ff;
                            color: #ffffff;
                            font-size: 16/@rem;
                            line-height: 38/@rem;
                            text-align: center;
                            span{
                                display: inline;
                            }
                        }
                        .message-verify:active{
                            background: #397bfe;
                        }
                    }
                }
                .config-message{
                    .jump-to-protocal{
                        color: #1863f9;
                        margin-top: 10/@rem;
                    }
                    .config-check{
                        display: flex;
                        align-items: center;
                        margin-top: 16/@rem;
                        .protocal{
                            font-size: 16/@rem;
                            margin-left: 10/@rem;
                            span{
                                color: #ff2832;
                            }
                        }
                    }
                }
                .config-btn{
                    width: 496/@rem;
                    height: 75/@rem;
                    background: #6699ff;
                    color: #fff;
                    display: flex;
                    align-items: center;
                    justify-content: center;
                    margin-top: 40/@rem;
                }
                .config-btn:active{
                    background: #397bfe;
                }
                .readed{
                    background: #a4a4a4;
                    color: #ffffff;
                }
                .readed:active{
                    background: #a4a4a4;
                }
            }
            .close-layer{
                margin-top: 20/@rem;
                img{
                    width: 100/@rem;
                    height: 100/@rem;
                }
            }
        }
        .member-layer{
            position: fixed;
            left: 100/@rem;
            top: 360/@rem;
            z-index: 99;
            width: 550/@rem;
            .distribution{
                width: 550/@rem;
                height: 525/@rem;
                border-radius: 30/@rem;
                background: #fff;
                display: flex;
                flex-direction: column;
                align-items: center;
                .distribution-head{
                    margin-top: 20/@rem;
                    span{
                        font-size: 36/@rem;
                        letter-spacing: 4/@rem;
                        font-weight: 500;
                        color: transparent;
                        background-image: linear-gradient(180deg, 
                        #35a9fe 0%, 
                        #916ffe 85%, 
                        #ed34fe 100%), 
                        linear-gradient(
                            #5b5b5b, 
                            #5b5b5b);
                        -webkit-background-clip: text;
                        -webkit-text-fill-color: transparent;
                    }
                }
                .distribution-form{
                    margin-top: 20/@rem;
                    .input-msg{
                        width: 490/@rem;
                        height: 76/@rem;
                        box-sizing: border-box;
                        padding-top: 16/@rem;
                        display: flex;
                        align-items: center;
                        border-bottom: 1px solid #cccccc;
                        span{
                            display: inline-block;
                            width: 144/@rem;
                            text-align: start;
                        }
                        input{
                            height: 30/@rem;
                            width: 330/@rem;
                            font-size: 24/@rem;
                            border: none;
                            outline: none;
                        }
                    }
                    .input-msg:nth-of-type(2){
                        input{
                            width: 170/@rem;
                            margin-right: 20/@rem;
                            border: none;
                            outline: none;
                        }
                        .message-verify{
                            width: 148/@rem;
                            height: 38/@rem;
                            border-radius: 4/@rem;
                            background: #6699ff;
                            color: #ffffff;
                            font-size: 16/@rem;
                            line-height: 38/@rem;
                            text-align: center;
                            span{
                                display: inline;
                            }
                        }
                        .message-verify:active{
                            background: #397bfe;
                        }
                    }
                }
                .config-message{
                    margin-top: 20/@rem;
                    .jump-to-protocal{
                        color: #1863f9;
                        margin-top: 10/@rem;
                    }
                    .config-check{
                        display: flex;
                        align-items: center;
                        margin-top: 16/@rem;
                        .protocal{
                            font-size: 16/@rem;
                            margin-left: 10/@rem;
                            span{
                                color: #ff2832;
                            }
                        }
                    }
                }
                .config-btn{
                    width: 496/@rem;
                    height: 75/@rem;
                    background: #6699ff;
                    color: #fff;
                    display: flex;
                    align-items: center;
                    justify-content: center;
                    margin-top: 40/@rem;
                }
                .config-btn:active{
                    background: #397bfe;
                }
                .readed{
                    background: #a4a4a4;
                    color: #ffffff;
                }
                .readed:active{
                    background: #a4a4a4;
                }
            }
            .close-layer{
                margin-top: 20/@rem;
                img{
                    width: 100/@rem;
                    height: 100/@rem;
                }
            }
        }
        .me-header{
            width: 100%;
            height: 370/@rem;
            position: relative;
            .me-header-bg{
                width: 750/@rem;
                height: 370/@rem;
            }
            .user-msg{
                width: 750/@rem;
                height: 370/@rem;
                position: absolute;
                left: 0;
                top: 0;
                .user-avatar{
                    display: flex;
                    flex-direction: column;
                    align-items: center;
                    margin-top: 20/@rem;
                    .avatar{
                        width: 136/@rem;
                        height: 136/@rem;
                        overflow: hidden;
                        border-radius: 50%;
                        border: 3/@rem solid #ffffff;
                        display: flex;
                        justify-content: center;
                        align-items: center;
                        img{
                            height: 136/@rem;
                        }
                    }
                    p{
                        font-size: 28/@rem;
                        color: #ffffff;
                        margin-top: 10/@rem;
                    }
                }
                .user-rights{
                    width: 650/@rem;
                    height: 150/@rem;
                    background: #fff;
                    border-radius: 10/@rem;
                    margin: 0 auto;
                    margin-top: 6/@rem;
                    display: flex;
                    align-items: center;
                    overflow: hidden;
                    .rights{
                        width: 25%;
                        height: 100%;
                        box-sizing: border-box;
                        padding-bottom: 20/@rem;
                        display: flex;
                        flex-direction: column;
                        align-items: center;
                        justify-content: flex-end;
                        span{
                            font-size: 28/@rem;
                            color: #333333;
                            margin-top: 20/@rem;
                        }
                    }
                    .rights:nth-of-type(1){
                        span:nth-of-type(1){
                            font-size: 36/@rem;
                            color: #ec4949;
                        }
                    }
                    .rights:nth-of-type(3){
                        img{
                            width: 68/@rem;
                            height: 48/@rem;
                        }
                    }
                    .rights:nth-of-type(5){
                        img{
                            width: 60/@rem;
                            height: 50/@rem;
                        }
                    }
                    .rights:nth-of-type(7){
                        img{
                            width: 67/@rem;
                            height: 64/@rem;
                        }
                    }
                    .line{
                        width: 1px;
                        height: 50/@rem;
                        background-color: #f0f0f0;
                    }
                }
            }
        }
        .me-store{
            width: 100%;
            background: #fff;
            margin-top: 10/@rem;
            .store-header{
                width: 100%;
                height: 88/@rem;
                display: flex;
                justify-content: space-between;
                align-items: center;
                border: 1px solid #f0f0f0;
                span{
                    font-size: 28/@rem;
                    color: #333333;
                    margin-left: 20/@rem;
                }
                .all-store{
                    height: 100%;
                    display: flex;
                    align-items: center;
                    margin-right: 20/@rem;
                    span{
                        margin: 0;
                        color: #868686;
                    }
                    img{
                        width: 36/@rem;
                        height: 36/@rem;
                        vertical-align: bottom;
                        margin-left: 16/@rem;
                    }
                }
            }
            .store-list{
                width: 100%;
                display: flex;
                .classify{
                    width: 20%;
                    height: 138/@rem;
                    background: #fff;
                    display: flex;
                    flex-direction: column;
                    align-items: center;
                    justify-content: center;
                    img{
                        width: 44/@rem;
                        height: 40/@rem;
                    }
                    span{
                        margin-top: 15/@rem;
                    }
                }
            }
        }
        .me-others{
            width: 100%;
            margin-top: 14/@rem;
            background: #fff;
            .others{
                width: 100%;
                height: 88/@rem;
                display: flex;
                justify-content: space-between;
                border-bottom: 1px solid #f0f0f0;
                .left{
                    height: 100%;
                    display: flex;
                    align-items: center;
                    margin-left: 20/@rem;
                    .img-box{
                        display: flex;
                        align-items: center;
                        width: 66/@rem;
                        height: 100%;
                    }
                    span{
                        font-size: 28/@rem;
                    }
                }
                .right{
                    width: 76/@rem;
                    height: 100%;
                    display: flex;
                    align-items: center;
                    justify-content: center;
                    img{
                        height: 36/@rem;
                        width: 36/@rem;
                    }
                }
            }
            .others:nth-of-type(1){
                .img-box{
                    img{
                        width: 34/@rem;
                        height: 38/@rem;
                    }
                }
            }
            .others:nth-of-type(2){
                .img-box{
                    img{
                        width: 36/@rem;
                        height: 36/@rem;
                    }
                }
            }
            .others:nth-of-type(3){
                .img-box{
                    img{
                        width: 46/@rem;
                        height: 44/@rem;
                    }
                }
            }
        }
    }
}
</style>
